首页 > 试题广场 >

字符串长度最大乘积

[编程题]字符串长度最大乘积
  • 热度指数:7029 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
已知一个字符串数组words,要求寻找其中两个没有重复字符的字符串,使得这两个字符串的长度乘积最大,输出这个最大的乘积。如:
words=["abcd","wxyh","defgh"], 其中不包含重复字符的两个字符串是"abcd"和"wxyh",则输出16
words=["a","aa","aaa","aaaa"], 找不到满足要求的两个字符串,则输出0

数据范围:输入的字符串长度满足 ,保证只包含小写字母

输入描述:
Input:

["a","ab","abc","cd","bcd","abcd"]


输出描述:
Output:

4
示例1

输入

["a","ab","abc","cd","bcd","abcd"]

输出

4

备注:
Input中,不包含相同字符的有三对:
"ab"和"cd"
"a"和"cd"
"a"和"bcd"
所以字符串长度乘积的最大值是4
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void (async function () {
    // Write your code here
    let line = await readline();
    let arr=line.replace(/[\[""\]]/g,'').split(',')  //去掉字符串的[]""
    // let arr=line.slice(1,-1).split(',').map(v=>v.slice(1,-1))
    let max = 0;
    for (let i = 0; i < arr.length; i++) {
        for (let j = i + 1; j < arr.length; j++) {
            if (arr[i].split("").every((v) => !arr[j].includes(v))) {
                max = Math.max(max, arr[i].length * arr[j].length);
            }
        }
    }
    console.log(max);
})();

发表于 2024-01-06 00:40:59 回复(0)